package com.corticon.client.util;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.corticon.client.model.DialogReportInput;
import com.corticon.client.model.DialogRequestInput;
import com.corticon.client.model.NavigatePageInput;
import com.corticon.client.model.ProcessResponseInput;
import com.corticon.client.model.PutRuleInput;


public class ApiRequestUtil {
	public DialogRequestInput getDialogReqParameter(HttpServletRequest request) {
		DialogRequestInput dialogRequest = new DialogRequestInput();
		String mode = "";
		String functions = "";
		String region = "";
		String productLine = "";
		String locale = "";
		String status = "";
		String effDate = "";
		String sessionId = "";
		String dialogInstanceId = "";
		if (request.getParameter("dialogInstanceId") != null) {
			dialogInstanceId = request.getParameter("dialogInstanceId");
		}
		if (request.getParameter("mode") != null) {
			mode = request.getParameter("mode");
		}
		if (request.getParameter("functions") != null) {
			functions = request.getParameter("functions");
		}
		if (request.getParameter("region") != null) {
			region = request.getParameter("region");
		}
		if (request.getParameter("product") != null) {
			productLine = request.getParameter("product");
		}
		if (request.getParameter("locale") != null) {
			locale = request.getParameter("locale");
		}
		if (request.getParameter("effDate") != null) {
			effDate = request.getParameter("effDate");
			effDate = effDate.replaceAll("/", "");
		}
		if (request.getParameter("sessionId") != null) {
			sessionId = request.getParameter("sessionId");
		}
		if (request.getParameter("status") != null) {
			status = request.getParameter("status");
		}
		dialogRequest.setDialogInstanceId(dialogInstanceId.equals("") ? "0" : dialogInstanceId);
		dialogRequest.setEffDate(effDate);
		dialogRequest.setFunctions(functions);
		dialogRequest.setLocale(locale);
		dialogRequest.setMode(mode);
		dialogRequest.setProduct(productLine);
		dialogRequest.setRegion(region);
		dialogRequest.setSessionId(sessionId);
		dialogRequest.setStatus(status);
		return dialogRequest;
	}

	public NavigatePageInput getNavigatePageParameter(HttpServletRequest request) {
		NavigatePageInput navigatePage = new NavigatePageInput();
		String dialogInstanceId = null;
		String dialogId = null;
		String pageId = null;
		String navigation = "";
		if (request.getParameter("dialogInstanceId") != null) {
			dialogInstanceId = request.getParameter("dialogInstanceId");

		}
		if (request.getParameter("dialogId") != null) {
			dialogId = request.getParameter("dialogId");

		}
		if (request.getParameter("pageId") != null) {
			pageId = request.getParameter("pageId");

		}
		if (request.getParameter("navigation") != null) {
			navigation = request.getParameter("navigation");
		}
		navigatePage.setDialogId(dialogId);
		navigatePage.setDialogInstanceId(dialogInstanceId);
		navigatePage.setNavigation(navigation);
		navigatePage.setPageId(pageId);
		return navigatePage;
	}

	public DialogReportInput getDialogReportParameter(HttpServletRequest request) {
		DialogReportInput dialogReportInput = new DialogReportInput();
		String dialogInstanceId = "0";
		String sessionId = null;
		if (request.getParameter("dialogInstanceId") != null) {
			if (!request.getParameter("dialogInstanceId").equalsIgnoreCase(null)) {
				dialogInstanceId = request.getParameter("dialogInstanceId");
			}
		}
		if (request.getParameter("sessionId") != null) {
			sessionId = request.getParameter("sessionId");
		}
		if (sessionId!=null && sessionId.equals("")){
			sessionId = null;
		}
		dialogReportInput.setDialogInstanceId(dialogInstanceId);
		dialogReportInput.setSessionId(sessionId);
		return dialogReportInput;
	}
	public ProcessResponseInput getProcessResponseInput(HttpServletRequest request){
		ProcessResponseInput processResponseInput = new ProcessResponseInput();
		String questionData = null;
		if (request.getParameter("questionData") != null) {
			questionData = request.getParameter("questionData");
		}
		if (questionData == null) {
			return null;
		}
		processResponseInput.setQuestionData(questionData);
		return processResponseInput;
	}
	public PutRuleInput getProcessResponseParameter(ProcessResponseInput processResponseInput) {
		Long dialogInstanceId = null;
		Long dialogId = null;
		Long pageId = null;
		Long questionId = null;		
		String allQuestion = null;
		Boolean isNextPage = false;		
		if (processResponseInput == null) {
			return null;
		}
		try {
			JSONArray putRules = new JSONArray(processResponseInput.getQuestionData());
			JSONObject questionJSON = putRules.getJSONObject(0);
			dialogInstanceId = questionJSON.getLong("dialogInstanceId");
			dialogId = questionJSON.getLong("dialogId");
			pageId = questionJSON.getLong("pageId");
			questionId = questionJSON.getLong("lastquestionId");
			allQuestion = questionJSON.getString("response");
			isNextPage = questionJSON.getBoolean("isNextPage");

			PutRuleInput putRuleInput = new PutRuleInput();
			putRuleInput.setDialogInstanceId(dialogInstanceId);
			putRuleInput.setDialogId(dialogId);
			putRuleInput.setPageId(pageId);
			putRuleInput.setQuestionId(questionId);
			putRuleInput.setIsNextPage(isNextPage);
			if (allQuestion == null || allQuestion.equals("")) {
				putRuleInput.setQuestionsData(null);
			} else {
				putRuleInput.setQuestionsData(changeJSONToList(dialogInstanceId, dialogId, allQuestion));
			}
			return putRuleInput;
		} catch (JSONException e) {
			return null;
		}
	}

	public List<PutRuleInput> changeJSONToList(Long dialoginstanceId, Long dialogId, String questionData) throws JSONException {
		List<PutRuleInput> questionsData = new ArrayList();
		JSONArray questions = new JSONArray(questionData);
		for (int i = 0; i < questions.length(); i++) {
			JSONObject questionJSON = questions.getJSONObject(i);
			PutRuleInput putRuleInput = new PutRuleInput();
			String pageId = questionJSON.getString("pageId");
			String questionId = questionJSON.getString("questionId");
			String pageinquestionId = questionJSON.getString("pageinquestionId");
			String responseValue = questionJSON.getString("responseValue");
			try {
				String responseid = questionJSON.getString("answerId") == null ? "" : questionJSON.getString("answerId");
				putRuleInput.setResponseId(responseid);
			} catch (JSONException e) {

			}

			putRuleInput.setAnswerId(responseValue);
			putRuleInput.setDialogId(dialogId);
			putRuleInput.setDialogInstanceId(dialoginstanceId);
			putRuleInput.setPageId(Long.valueOf(pageId));
			putRuleInput.setQuestionId(Long.valueOf(questionId));
			putRuleInput.setPageinquestionId(Long.valueOf(pageinquestionId));

			questionsData.add(putRuleInput);
		}
		return questionsData;
	}
}
